import java.sql.SQLOutput;

public class MyLinkedList {
    static class ListNode{
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int data){
            this.val = data;
        }
    }

    public ListNode head;
    public ListNode last;

    // 头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    // 尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            last = node;
        }else{
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    public void addIndex(int index, int data){
        if(index < 0 || index > size()){
            throw new RuntimeException(index + "位置不合法！");
        }
        if(index == 0){
            addFirst(data);
            return;
        }else if(index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode prevNode = findPrev(index);
        ListNode nextNode = prevNode.next;
        prevNode.next = node;
        node.prev = prevNode;
        node.next = nextNode;
        nextNode.prev = node;
    }
    private ListNode findPrev(int index){
        ListNode cur = head;
        while(--index > 0){
            cur = cur.next;
        }
        return cur;
    }

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size(){
        ListNode cur = head;
        int count = 0;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 删除第一次出现关键字 key 的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                ListNode prevNode = cur.prev;
                ListNode nextNode = cur.next;
                if(prevNode != null) {
                    prevNode.next = nextNode;
                }else{
                    head = head.next;
                    if(head != null){
                        head.prev = null;
                    }
                }
                if(nextNode != null) {
                    nextNode.prev = prevNode;
                }else{
                    last = last.prev;
                    if(last != null){
                        last.next = null;
                    }
                }
                break;
            }
            cur = cur.next;
        }
    }

    // 删除所有值为 key 的节点
    public void removeAll(int key){
        ListNode cur = head;
        while(cur != null) {
            if (cur.val == key) {
                ListNode prevNode = cur.prev;
                ListNode nextNode = cur.next;
                if (prevNode != null) {
                    prevNode.next = nextNode;
                } else {
                    head = head.next;
                    if(head != null){
                        head.prev = null;
                    }
                }
                if (nextNode != null) {
                    nextNode.prev = prevNode;
                } else {
                    last = last.prev;
                    if(last != null){
                        last.next = null;
                    }
                }
                cur = nextNode;
            }else{
                cur = cur.next;
            }
        }
    }

    public void clear(){
        ListNode cur = head;
        while(cur != null){
            ListNode next = cur.next;
            cur = null;
            cur = next;
        }
        head = null;
        last = null;
    }
}
